class EntitiesController < ApplicationController
  # GET /entities
  # GET /entities.xml
  
  before_filter :get_museum
  
  def editing
    @acquisition_methods = AcquisitionMethod.find(:all)
    @countries = Country.find(:all)
    
  end
  
  def index
    @entities = @museum.entities

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @entities.to_xml }
    end
  end

  # GET /entities/1
  # GET /entities/1.xml
  def show
    @entity = Entity.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @entity.to_xml }
    end
  end

  def import
    
  end


  # GET /entities/new
  def new
    editing
    @entity = Entity.new
  end

  # GET /entities/1;edit
  def edit
    editing
    @entity = Entity.find(params[:id])
  end

  # POST /entities
  # POST /entities.xml
  def create
    editing
    
    if params[:objects]
      @file = params[:objects].read  
        @xml = REXML::Document.new(@file)
        @count = 0
        @errors = 0
        @error_message = ""
        @message = ""
        @xml.root.elements.each do |r|


          @entity = Entity.new
          @entity.museum_id = @museum.id

          if r.elements["country_of_origin"]
            @country = Country.find_by_iso_code(r.elements["country_of_origin"].attributes["iso_code"])
            if @country
              @entity.country_id = @country.id
            end
          end
          
          @entity.name = r.elements['name'].text
          if r.elements['acquisition']
            if r.elements['acquisition'].elements["date"]
              unless r.elements['acquisition'].elements["date"].attributes["year"] == ""
                @entity.acquired_year = r.elements['acquisition'].elements["date"].attributes["year"]
              end
            end  
            
            if r.elements['acquisition'].elements['method']
              @acquisition_method = AcquisitionMethod.find_or_create(:name => r.elements['acquisition'].elements['method'].text)
              @entity.acquisition_method_id = @acquisition_method.id
            end

               if r.elements['acquisition'].elements['type']

                   @object_type = ObjectType.find_or_create(:name => r.elements['acquisition'].elements['type'].text)

                 
             end

            
          end
          
            
          if r.elements['collection']
            @entity.collection = Collection.find_or_create(:name => r.elements['collection'].text, :museum_id => @museum.id)
          end  


         
         @entity.museum_identifier = r.elements['identifier'].text

           if @entity.save
             if @object_type
                @categorisation = Categorisation.new
                @categorisation.object_type = @object_type
                @categorisation.entity_id = @entity.id
                @categorisation.save
             end
             @count += 1
           else
             @entity.errors.each do |e|

              @error_message += e[0]
             end
              @errors += 1
           end

             


       end    

     #  flash[:notice] = @count.to_s + " objects saved." + @errors.to_s + " errors." + @error_message
      render :action => :index
         
    else

      @entity = Entity.new(params[:entity])
      @entity.museum = @museum



      respond_to do |format|
        if @entity.save
          flash[:notice] = 'Entity was successfully created.'
          format.html { redirect_to museum_object_path(@museum.id, @entity) }
          format.xml  { head :created, :location => entity_url(@entity) }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @entity.errors.to_xml }
        end
      end
      
    end


  end

  # PUT /entities/1
  # PUT /entities/1.xml
  def update
    editing
    @entity = Entity.find(params[:id])

    respond_to do |format|
      if @entity.update_attributes(params[:entity])
        flash[:notice] = 'Entity was successfully updated.'
        format.html { redirect_to museum_object_url(@museum, @entity) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @entity.errors.to_xml }
      end
    end
  end

  # DELETE /entities/1
  # DELETE /entities/1.xml
  def destroy
    @entity = Entity.find(params[:id])
    @entity.destroy

    respond_to do |format|
      format.html { redirect_to entities_url }
      format.xml  { head :ok }
    end
  end
  
  private
  
  def get_museum
    @museum = Museum.find(params[:museum_id])
  end
  
end
